//
//  LHRadioDownLoadController.m
//  EPA
//
//  Created by 卢浩 on 16/4/27.
//  Copyright © 2016年 卢浩. All rights reserved.
//

#import "LHRadioDownLoadController.h"
#import "LHRadioDownLoadManager.h"
#import "LHRadioDownloadCell.h"
#import "LHRadioDidDownTableViewController.h"

@interface LHRadioDownLoadController ()

@end

@implementation LHRadioDownLoadController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"电台下载列表";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(backAction)];
    
    UIBarButtonItem *showDidDownBarBtn = [[UIBarButtonItem alloc]initWithTitle:@"已下载" style:(UIBarButtonItemStylePlain) target:self action:@selector(showDidDownAction)];
    self.navigationItem.rightBarButtonItem = showDidDownBarBtn;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LHRadioDownloadCell" bundle:nil] forCellReuseIdentifier:@"LHRadioDownloadCell"];
    
    //接收通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData) name:@"downloadReloadData" object:nil];
}

- (void)reloadData{
    [self.tableView reloadData];
}

- (void)showDidDownAction{
    LHRadioDidDownTableViewController *didDownVC = [[LHRadioDidDownTableViewController alloc]init];
    [self.navigationController pushViewController:didDownVC animated:YES];
}

- (void)backAction{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[LHRadioDownloadManager shareManager] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LHRadioDownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LHRadioDownloadCell" forIndexPath:indexPath];
    cell.indexPath = indexPath;
    return cell;
}

@end
